'''
使用python -requests包进行接口测试。
接口测试的原理：构造不同的参数，调用接口发送请求，对响应的
'''
import requests
base_url = "http://192.168.150.84:8088//futureloan/mvc/api/"
'''
发送get请求，不带参数
'''
url = base_url+"member/list" #获取用户列表的接口
r = requests.get(url) #发送请求
print(r.text) # 打印响应
r_json = r.json()# 如果响应结果是json，可以转成json格式的
# 对请求做断言
# 把json当做一个字典来使用，判断实际结果与预期结果是否相同
# r_json['msg'] 实际结果，接口返回的响应信息
# '获取用户列表成功‘，预期结果，从接口文档中来
assert r_json['msg'] == '获取用户列表成功'
assert r_json['code'] == '10001'
assert r_json['status'] == 1

'''
get请求带参数：
1.拼接到URL后面，多个参数用and连接起来
'''
url = base_url+'member/register?mobilephone=18012345678&pwd=123456' # 注册接口
r= requests.get(url)
print(r.json())
assert r.json()['msg'] == '手机号码已被注册'
assert r.json()['status'] == 0
assert r.json()['code'] == '20110'
url = base_url+'member/register?mobilephone=18012345678&pwd=123' # 注册接口
r= requests.get(url)
print(r.json())
assert r.json()['msg'] == '密码长度必须为6~18'
assert r.json()['status'] == 0
assert r.json()['code'] == '20108'
'''
get请求带参数：
2.使用params传参
'''
url = base_url+'member/register'
cs = {"mobilephone":1801234567,"pwd":1234123212142323213}
r= requests.get(url,params=cs)
print(r.json())
assert r.json()['msg'] == '密码长度必须为6~18'
assert r.json()['status'] == 0
assert r.json()['code'] == '20108'
# 响应
print(r.status_code) # 响应码
print(r.reason) # 响应状态原因
print(r.text) # 文本格式的响应
print(r.encoding) # 编码方式
print(r.request.headers) #请求头
print(r.headers) # 响应头

'''
get请求，这只请求头伪装成浏览器发送的请求
'''
url = base_url+'member/register'
cs = {"mobilephone":1801234567,"pwd":1234123212142323213}
head = {
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
}
r= requests.get(url,params=cs,headers=head)
print(r.json())
print(r.request.headers)



